package com.dt.global.web.action.Repquery;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.context.annotation.Scope;

import com.dt.components.exstruts.action.BaseActionSupport;
import com.dt.global.entitys.bo.TWebCoreMien;
import com.dt.global.entitys.bo.TWebLinks;
import com.dt.global.utils.Proerties;
import com.dt.global.web.mar.repquery.RepQueryMar;

/**
 * 代表覆职action
 * @author MWG
 *
 */
@SuppressWarnings("serial")
@Scope("prototype")
public class RepQueryAction extends BaseActionSupport{
	//json解析对象
	public ObjectMapper mapper=new ObjectMapper();
	@Resource
	private RepQueryMar repQueryMar;
	/**
	 * 根据栏目id查询代表风采(ajax)
	 */
	public String getTWebRepQueryAjax(){
		HttpServletResponse resp = 
			ServletActionContext.getResponse();
		resp.setContentType("text/html;utf-8");
		resp.setCharacterEncoding("utf-8");
		try {
			String channelId = getRequest().getParameter("channelId");
			Proerties popUtil = new Proerties();
			String url = popUtil.getValue("wjrdht");
			if(channelId !=null&!channelId.equals("")){
				List<Map<String,String>> list = new ArrayList<Map<String,String>>();
				List<TWebCoreMien> reps= repQueryMar.getTWebRepQuery(channelId);
				//声明Map对象
				Map<String,String> map = null;
				if(reps.size()>0){
					for(TWebCoreMien rep : reps){
						map = new HashMap<String, String>();
						map.put("articleId", rep.getArticleId());
						map.put("channelId", channelId);
						map.put("name", rep.getName());
						if(rep.getTitleImg()!=null&&rep.getTitleImg().length()>0){
						map.put("titleImg",url+"/uploadfile/"+rep.getTitleImg());
						}else{
						map.put("titleImg","");	
						}
						list.add(map);
					}
				}
				String json = mapper.writeValueAsString(list);
				PrintWriter out  =resp.getWriter();
				out.print(json);
				out.flush();
				out.close();
				return null;
			}else{
				return "error";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}

}
